package com.swl.juc.Executor;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchExecutorTest2 {
    private static int sum = 0;

    public void doSome() {
        System.out.println(Thread.currentThread() + "开始" + System.currentTimeMillis());
        long start = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        CountDownLatch latch = new CountDownLatch(3);
        executorService.execute(() -> {
            //第一个子任务
            sum++;
            latch.countDown();
            System.out.println(Thread.currentThread().getName()+":1");
        });

        executorService.execute(() -> {
            //第二个子任务
            sum += 2;
            latch.countDown();
            System.out.println(Thread.currentThread().getName()+":2");

        });

        executorService.execute(() -> {
            //第三个子任务
            sum += 3;
            latch.countDown();
            System.out.println(Thread.currentThread().getName()+":3");

        });
        //关闭线程池
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executorService.shutdown();
        System.out.println(Thread.currentThread() + "耗时" + (System.currentTimeMillis()-start));
    }

    public static void main(String[] args) {
        CountDownLatchExecutorTest2 executor = new CountDownLatchExecutorTest2();
        executor.doSome();
    }


}
